package cool.tianyou.blue.bird.rest;

import cool.tianyou.blue.bird.common.model.entity.RestApiResult;
import cool.tianyou.blue.bird.common.model.po.LoginUser;
import cool.tianyou.blue.bird.common.tool.UserHolder;
import cool.tianyou.blue.bird.common.util.RestResultUtil;
import cool.tianyou.blue.bird.model.NotificationMessage;
import cool.tianyou.blue.bird.module.synchronize.model.PushResult;
import cool.tianyou.blue.bird.module.synchronize.service.WebSocketService;
import java.util.List;
import java.util.Map;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Liutianyou
 * @date 2022/1/21 9:33 下午
 */
@RestController
@RequestMapping("/notification")
@RequiredArgsConstructor
public class NotificationController {

  private final WebSocketService webSocketService;
  @PostMapping("/push")
  public ResponseEntity<RestApiResult<List<String>>> push(
      @RequestBody Map<Integer, NotificationMessage> dispatchMessage) {
    PushResult pushResult = webSocketService
        .sendMessage(UserHolder.getLoginUser(), dispatchMessage);

    return RestResultUtil.ok(pushResult.getAlias());
  }
}
